package com.yidu.hbatsft.service.impl;

import com.github.pagehelper.PageHelper;
import com.yidu.hbatsft.dao.AtsDataDicMapper;
import com.yidu.hbatsft.domain.AtsData;
import com.yidu.hbatsft.domain.AtsDataDic;
import com.yidu.hbatsft.service.AtsDataDicService;
import com.yidu.utils.RedisUtil;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.Random;

/**
 *
 * @author hp
 */
@Service
public class AtsDataDicServiceImpl implements AtsDataDicService {
    //数据字典dao
    @Resource
    AtsDataDicMapper ddicDao;

    /**
     * 查询所有
     * @param dname
     * @param dtype
     * @param dcode
     * @param statr
     * @param rows
     * @return
     */
    @Override
    public List<AtsDataDic> selectAll(String dname, String dtype,String dcode,Integer statr,Integer rows) {
        List<AtsDataDic> list =ddicDao.selectAll(dname,dtype,dcode,statr,rows);
        return list;
    }

    /**
     * 总行数
     * @return
     */
    @Override
    public int count(String dname, String dtype,String dcode) {
        return ddicDao.count(dname,dtype,dcode);
    }

    /**
     * 修改状态
     * @param did
     * @return
     */
    @Override
    public int updateId(Integer did) {
        AtsDataDic atsDataDic = new AtsDataDic();
        atsDataDic.setDid(did);
        atsDataDic.setIsva(0);
        redisUtil.deleteByPrex("hbats_dic_"); //删除缓存
        return ddicDao.updateByPrimaryKeySelective(atsDataDic);
    }

    /**
     * 根据Id查询
     * @param did
     * @return
     */
    @Override
    public AtsDataDic findById(Integer did) {
        AtsDataDic ddic = ddicDao.findById(did);
        return ddic;
    }

    /**
     * 新增
     * @param atsDataDic
     * @return
     */
    @Override
    public int insertSelective(AtsDataDic atsDataDic) {
        atsDataDic.setIsva(1);

        atsDataDic.setCreattime(new Date());
        int row = ddicDao.insertSelective(atsDataDic);
        redisUtil.deleteByPrex("hbats_dic_"); //删除缓存
        return row;
    }

    /**
     * 修改
     * @param atsDataDic
     * @return
     */
    @Override
    public int updateByPrimaryKeySelective(AtsDataDic atsDataDic) {
        AtsDataDic ddic = ddicDao.findById(atsDataDic.getDid());
        atsDataDic.setIsva(1);
        atsDataDic.setCreater(ddic.getCreater());
        atsDataDic.setCreattime(ddic.getCreattime());
        redisUtil.deleteByPrex("hbats_dic_"); //删除缓存
        return ddicDao.updateByPrimaryKeySelective(atsDataDic);
    }

    @Resource
    RedisUtil redisUtil;
    /**
     * 根据字典编码查询
     * @param dcode
     * @return
     */
    @Override
    public List<AtsDataDic> selectDcode(String dcode) {
        List<AtsDataDic> list = (List<AtsDataDic>)redisUtil.get("hbats_dic_"+dcode);

        if(list==null){
            list = ddicDao.selectDcode(dcode);
            redisUtil.set("hbats_dic_"+dcode,list);
        }
        return list;
    }
}
